/* Import data */
clear all

/* Start logging the process */
log using "$logs\t-test_rejected.log", replace

/* Load dataset containing rejected FinTech loan applicants */
use "$output\rejected_agg_industries_modified.dta", clear

/*----------------------------------------------------------------------------*/
/* Generate a dummy variable for whether a firm has a credit line (co > 0)    */
/*----------------------------------------------------------------------------*/
preserve
    keep if delta < 0 // Keep only past observations
    collapse (sum) co, by(siren) // Aggregate the 'co' variable at the firm level
    gen codummy = co > 0 // Create a binary indicator for credit line presence
    drop co // Drop the original variable
    save "$output\data_co_dummy.dta", replace
restore

/*----------------------------------------------------------------------------*/
/* Keep only the year before the new IFP loan application or rejection        */
/*----------------------------------------------------------------------------*/
keep if delta == -11

/* Save the list of firms for reference */
preserve
    save "$output\rejected_ttest_siren_list.dta", replace
restore

/*----------------------------------------------------------------------------*/
/* Generate Summary Statistics for Analysis                                   */
/*----------------------------------------------------------------------------*/

/* Define labels for accepted (FinTech) and rejected firms */
label define accepted 0 "Rejected" 1 "FinTech"
label values accepted accepted

/* Generate firm type indicators for analysis */
capture drop firm_type type_reverse ratingint
gen firm_type = accepted
gen type_reverse = 1 - firm_type // Reverse coding for comparisons

/* Adjust rating variable for numerical processing */
gen ratingint = rating - 1
replace ratingint = 13 if ratingint == 0  

/* Merge with credit line dummy dataset */
merge 1:1 siren using "$output\data_co_dummy.dta"
keep if _merge == 3 // Keep only matched firms
drop _merge

/*----------------------------------------------------------------------------*/
/* Table 3: Comparing Accepted (FinTech) vs Rejected Firms                    */
/*----------------------------------------------------------------------------*/

/* Label variables for clarity */
label var ageatevent         "Age"
label var r1filled           "Working capital"
label var r3filled           "EBIT"
label var r5filled           "Sales"
label var r12filled          "Investment"
label var r13filled          "Debt"
label var logassets_filled   "Total Assets"
label var logemployees       "Employment"
label var r_collateral_filled "Tangible assets"
label var rdcostsfilled      "R&D costs filled"
label var ratingint          "Rating"
label var codummy            "$\mathbbm{1}$(Credit line)"

/* Define firm characteristics for statistical comparison */
global firm_var `"ratingint r_collateral_filled logemployees r13filled r12filled r3filled r1filled ageatevent logassets_filled codummy"'

/* Compute summary statistics for accepted and rejected firms */
eststo bank: estpost tabstat $firm_var if firm_type == 0, s(mean) c(statistics)
eststo bank_accepted: estpost tabstat $firm_var if firm_type == 1, s(mean) c(statistics)
eststo t_test01: estpost ttest $firm_var, by(type_reverse)
eststo sum1: estpost sum $firm_var if firm_type == 1
eststo sum0: estpost sum $firm_var if firm_type == 0

/* Format and export results to LaTeX table */
esttab bank_accepted bank t_test01 t_test01 sum1 sum0 using "$tables\Table_3c_Comparing_FinTech_accepted_rejected_candidates_pre_PSM.tex", ///
stats(N, label(`"N"') layout("") fmt(%9.0fc)) ///
replace nonote nogaps booktabs fragment label noobs nolines nomtitles ///
cells("mean(fmt(%9.3f) pattern(1 1 0 0 0 0)) b(fmt(%9.3f) pattern(0 0 1 0 0 0)) t(fmt(%9.3f) pattern(0 0 0 1 0 0) star) count(fmt(%9.0fc) pattern(0 0 0 0 1 1))") ///
mgroups("(a) Accepted" "(b) Rejected" "(a)-(b)" "\emph{t}-statistic" "(a) Count" "(b) Count", pattern(1 1 1 1 1 1) ///
prefix(\multicolumn{@span}{c}{) suffix(}) span ) collabels(none) ///
posthead(\midrule)

/*----------------------------------------------------------------------------*/
/* Perform OLS regression for each firm characteristic variable               */
/*----------------------------------------------------------------------------*/
gen treated = accepted == 1 // Define treatment variable

local i = 0
foreach var of local `"logassets_filled ageatevent r1filled r3filled r12filled r13filled logemployees r_collateral_filled ratingint codummy"' {
    local i = `i' + 1  
    preserve
        egen `var'_std = std(`var')
        eststo ols_`var': reg `var'_std treated
        gen rank = `i'  
        gen B_ols = _b[treated]  
        gen S_ols = _se[treated]  
        keep rank B_ols S_ols
        keep if _n == 1
        save "$output\output_reject_ols_`i'.dta", replace
    restore
}

/*----------------------------------------------------------------------------*/
/* Perform Propensity Score Matching (PSM)                                    */
/*----------------------------------------------------------------------------*/

use "$output\rejected_psm.dta", clear  // Load PSM-matched dataset

preserve
    keep if delta < 0
    collapse (sum) co, by(siren)
    gen codummy = co > 0
    drop co
    save "$output\data_psm_co_dummy.dta", replace
restore

/* Keep firms one year before loan application or rejection */
keep if delta == -11

/* Define firm type and labels */
label define accepted 0 "Rejected" 1 "FinTech"
label values accepted accepted
gen firm_type = accepted
gen type_reverse = 1 - firm_type

/* Adjust rating variable for numerical processing */
gen ratingint = rating - 1
replace ratingint = 13 if ratingint == 0  

/* Merge matched dataset with credit line dummy */
merge 1:1 siren using "$output\data_co_dummy.dta"
keep if _merge == 3
drop _merge

/*----------------------------------------------------------------------------*/
/* Table 3 (Post-PSM): Comparing Accepted vs Rejected Firms                   */
/*----------------------------------------------------------------------------*/

/* Expand dataset by weight for weighted comparisons */
preserve
    su weight, d
    gen max_weight = `r(max)'
    expand max_weight
    bys siren mdate: gen nb = _n
    keep if nb <= weight
    drop nb
	
/* Compute summary statistics after matching */
eststo bank: estpost tabstat $firm_var if firm_type == 0, s(mean) c(statistics)
eststo bank_accepted: estpost tabstat $firm_var if firm_type == 1, s(mean) c(statistics)
eststo t_test01: estpost ttest $firm_var, by(type_reverse)
eststo sum1: estpost sum $firm_var if firm_type == 1
eststo sum0: estpost sum $firm_var if firm_type == 0

/* Format and export post-PSM comparison table */
esttab bank_accepted bank t_test01 t_test01 sum1 sum0 using "$tables\Table_3d_Comparing_FinTech_accepted_rejected_candidates_post_PSM.tex", ///
stats(N, label(`"N"') fmt(%9.0fc)) ///
replace nonote nogaps booktabs fragment label noobs nolines nomtitles ///
cells("mean(fmt(%9.3f) pattern(1 1 0 0 0 0)) b(fmt(%9.3f) pattern(0 0 1 0 0 0)) t(fmt(%9.3f) pattern(0 0 0 1 0 0) star) count(fmt(%9.0fc) pattern(0 0 0 0 1 1))") ///
mgroups("(a) Accepted" "(b) Rejected" "(a)-(b)" "\emph{t}-statistic" "(a) Count" "(b) Count", pattern(1 1 1 1 1 1) ///
prefix(\multicolumn{@span}{c}{) suffix(}) span ) collabels(none) ///
posthead(\midrule)

restore

//------------------------------------------------------------------------------------//
//                T-test Visualizations: Accepted vs Rejected Firms                   //
//------------------------------------------------------------------------------------//

/* Define the list of firm characteristics to be analyzed */
local variables `"logassets_filled ageatevent r1filled r3filled r12filled r13filled logemployees r_collateral_filled ratingint codummy"' 

/* Define treatment variable: 1 if firm was accepted (FinTech), 0 if rejected */
gen treated = accepted == 1  

/*----------------------------------------------------------------------------*/
/* Compute OLS Regression for Each Variable in the List                      */
/*----------------------------------------------------------------------------*/

local i = 0 // Initialize counter
foreach var of local variables { 
	local i = `i' + 1  // Increment counter for each variable
	preserve
	    egen `var'_std = std(`var') // Standardize the variable
	    eststo ols_`var' : reg `var'_std treated [pw = weight] // Perform weighted OLS regression
	    gen rank =  `i'  // Store ranking index
	    gen B_wls = _b[treated]  // Store estimated coefficient
	    gen S_wls = _se[treated] // Store standard error
	    keep rank B_wls S_wls // Keep only relevant variables
	    keep if _n == 1 // Keep only one observation per variable
	    save "$output\output_reject_wls_`i'.dta", replace // Save individual results
	restore
}

/*----------------------------------------------------------------------------*/
/* Merge All Regression Results into a Single Dataset                        */
/*----------------------------------------------------------------------------*/

local max = `i'  // Store the number of variables processed

clear
save "$output\output_reject_ols", replace emptyok // Create an empty dataset for OLS results	
forval i = 1/`max' {
	sleep 1000  // Pause for 1 second to prevent errors in large loops
	append using "$output\output_reject_ols_`i'"  // Append OLS results for each variable
}	
save "$output\output_reject_ols", replace // Save the complete dataset

clear
save "$output\output_reject_wls", replace emptyok // Create an empty dataset for WLS results
forval i = 1/`max' {
	sleep 1000  // Pause for stability
	append using "$output\output_reject_wls_`i'" // Append WLS results for each variable
}	
save "$output\output_reject_wls", replace // Save the complete dataset

/*----------------------------------------------------------------------------*/
/* Compute Confidence Intervals for the Estimates                            */
/*----------------------------------------------------------------------------*/

clear 
use "$output\output_reject_ols"
merge 1:1 rank using "$output\output_reject_wls" // Merge OLS and WLS results

/* Compute confidence intervals for OLS and WLS estimates */
gen low_ols = B_ols - 1.96 * S_ols // Lower bound for OLS
gen high_ols = B_ols + 1.96 * S_ols // Upper bound for OLS
gen low_wls = B_wls - 1.96 * S_wls // Lower bound for WLS
gen high_wls = B_wls + 1.96 * S_wls // Upper bound for WLS

/* Adjust ranking for visualization purposes */
gen rank_ols = rank + 0.05 // Offset OLS points for better visibility
gen rank_wls = rank - 0.05 // Offset WLS points for better visibility

/*----------------------------------------------------------------------------*/
/* Generate a T-test Plot: Matched vs Unmatched Sample                        */
/*----------------------------------------------------------------------------*/

twoway 	(rcap high_ols low_ols rank_ols, lwidth(1) msize(0) lcolor(emerald) horizontal)  /*
	*/ (rcap high_wls low_wls rank_wls, lwidth(1) msize(0) lcolor(gs10%40) horizontal) /*
	*/ (scatter rank_ols B_ols, mfcolor(white) msize(1.8) mcolor(emerald)) /*
	*/ (scatter rank_wls B_wls, mfcolor(white) msize(1.8) msymbol(triangle) mcolor(gs10%40)) ,/*
	*/	graphregion(color(white)) xline(0) /*
	*/ legend(order(3 "Unmatched sample" 4 "Matched sample") cols(2) ) ylabel(,nogrid) /*
	*/ ylabel(1 "Total Assets" 2 "Age" 3 "Working capital" 4 "EBIT" 5 "Investment" 6 "Debt" 7 "Employment" 8 "Tangible assets" 9 "Rating"  10 "1(Credit line)" , angle(horizontal))

/* Export the graph */
graph export "$figures/Figure_2b_rejected_t_test_plot.pdf", replace

/* Close the log file */
log close
